﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Net;
using System.Xml;
using System.IO;
using System.Web;

namespace Readify.Management.Automation.GoGrid
{
    [Cmdlet("Add", "GoGridServer")]
    public class AddGoGridServerCmdlet : Cmdlet
    {
        [Parameter(Mandatory = true)]
        public PSCredential Credential { get; set; }

        [Parameter(Mandatory = true)]
        public string Name { get; set; }

        [Parameter(Mandatory = true)]
        public string Image { get; set; }

        [Parameter(Mandatory = true)]
        public string Memory { get; set; }

        [Parameter(Mandatory = true)]
        public string IPAddress { get; set; }

        [Parameter()]
        public string Description { get; set; }

        protected override void ProcessRecord()
        {
            string url = this.BuildUrl();

            XmlDocument document = GoGridHelper.ProcessRequest(url, this);
            XmlNodeList objectElementNodeList = document.SelectNodes("/gogrid/response/list/object[@name = 'server']");

            foreach (XmlNode objectElementNode in objectElementNodeList)
            {
                GoGridServer server = GoGridServer.CreateFromXmlNode(objectElementNode);
                this.WriteObject(server);
            }
        }

        private string BuildUrl()
        {
            string url = this.BuildBasicUrl();
            url = this.AppendNameModifier(url);
            url = this.AppendImageModifier(url);
            url = this.AppendMemoryModifier(url);
            url = this.AppendIPAddressModifier(url);
            url = this.AppendDescriptionModifierIfNecessary(url);
            return url;
        }

        private string AppendDescriptionModifierIfNecessary(string url)
        {
            if (string.IsNullOrEmpty(this.Description))
            {
                return url;
            }

            string escapedDescription = HttpUtility.UrlEncode(this.Description);
            url = string.Format("{0}&description={1}", url, escapedDescription);
            return url;
        }


        private string AppendNameModifier(string url)
        {
            string escapedName = HttpUtility.UrlEncode(this.Name);
            url = string.Format("{0}&name={1}", url, escapedName);
            return url;
        }

        private string AppendImageModifier(string url)
        {
            string escapedImage = HttpUtility.UrlEncode(this.Image);
            url = string.Format("{0}&image={1}", url, escapedImage);
            return url;
        }

        private string AppendMemoryModifier(string url)
        {
            string escapedMemory = HttpUtility.UrlEncode(this.Memory);
            url = string.Format("{0}&ram={1}", url, escapedMemory);
            return url;
        }

        private string AppendIPAddressModifier(string url)
        {
            string escapedIPAddress = HttpUtility.UrlEncode(this.IPAddress);
            url = string.Format("{0}&ip={1}", url, escapedIPAddress);
            return url;
        }


        private string BuildBasicUrl()
        {
            string urlFormat = "https://api.gogrid.com/api/grid/server/add?api_key={0}&sig={1}&v=1.0&format=xml";

            NetworkCredential credential = this.Credential.GetNetworkCredential();
            string apiKey = credential.UserName;
            string secret = credential.Password;

            string signature = GoGridHelper.GenerateRequestSignature(apiKey, secret);

            string basicUrl = string.Format(urlFormat, apiKey, signature);
            return basicUrl;
        }
    }
}
